package com.yx.controller;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Classname FileController
 * @Date 2021/07/08 20:52
 * @Author by yx
 * @Description TODO:
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FastFileStorageClient client;

    @Value("${fdfs.nginxHost}")
    private String nginxHost;

    @PostMapping("/upload")
    public String upload(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        long size = file.getSize();

        //获取文件后缀
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());

        StorePath storePath = client.uploadImageAndCrtThumbImage(inputStream, size, extension, null);
        String fullPath = storePath.getFullPath();
        System.out.println(fullPath);
        return nginxHost+fullPath;
    }
}
